package com.purengage.ipilih;

import org.json.JSONArray;
import org.json.JSONException;

import com.markupartist.android.widget.ActionBar;
import com.markupartist.android.widget.ActionBar.ActionText;
import com.markupartist.android.widget.ActionBar.IntentAction;
import com.purengage.ipilih.lib.DBClass;
import com.purengage.ipilih.lib.HttpConnect;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class AddStatus extends Activity {

	public class addstatus extends AsyncTask<String, String, Boolean> {
		private ProgressDialog dialog = new ProgressDialog(AddStatus.this);
		DBClass db = new DBClass(AddStatus.this);

		@Override
		protected Boolean doInBackground(String... params) {
			HttpConnect http = new HttpConnect(
					"http://purengage.com/api/statuses", 5);
			http.addVar("client_id",
					getResources().getString(R.string.client_id));
			http.addVar("token", db.getValue("token"));
			http.addVar("indent", "1");
			
			if(params[1].equalsIgnoreCase("Teman satu area kerja")){
				http.addVar("privacy","group");
			}
			
			String message = params[0].replace(" ", "%20");
			http.addVar("message", message);
			
			if(params[0].equalsIgnoreCase(""))
				return false;

			boolean res = http.postData();
			if (res) {
				try {
					JSONArray re = new JSONArray("[" + http.getOutput() + "]");
					Log.d("array", re.toString());
				} catch (JSONException e) {					
					e.printStackTrace();
				}
			}
			return res;
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();

			dialog.setMessage("Loading..");
			dialog.show();

		}

		@Override
		protected void onPostExecute(Boolean result) {			
			super.onPostExecute(result);
			dialog.dismiss();
			finish();
		}

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_addstatus);
		final ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar);
		actionBar.setTitle("Status");

		actionBar.setHomeAction(new IntentAction(this, Relawan
				.createIntent(this), R.drawable.ic_action_back));

		actionBar.addAction(new ActionText() {

			@Override
			public String getString() {
				return "Post";
			}

			@Override
			public void performActionText(View view) {
				//Toast.makeText(AddStatus.this, "Send Post.", Toast.LENGTH_SHORT)
				//		.show();
				PostMessage();

			}
		});
		LinearLayout privacy = (LinearLayout) findViewById(R.id.privacy);

		privacy.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				alertPrivacy();
			}
		});

	}

	public void PostMessage() {
		TextView t1 = (TextView) findViewById(R.id.textPrivacy);
		EditText message = (EditText) findViewById(R.id.editMessage);
		String pesan = message.getText().toString();
		String privasi = t1.getText().toString();
		String[] params = {pesan, privasi};
		new addstatus().execute(params);
	}

	public void alertPrivacy() {
		LayoutInflater inflater = getLayoutInflater();
		final View formElementView = inflater.inflate(R.layout.popup_pilihan,
				null);

		ContextThemeWrapper ctw = new ContextThemeWrapper(this,
				R.style.ThemeDialog);
		AlertDialog.Builder builder = new AlertDialog.Builder(ctw);
		View view = inflater.inflate(R.layout.dialog_customtitle, null);
		TextView tx = (TextView) view.findViewById(R.id.text_title);
		tx.setText("Post Privacy");
		final RadioGroup pilihan = (RadioGroup) formElementView
				.findViewById(R.id.RadioGroup);
		RadioButton r1 = (RadioButton) formElementView
				.findViewById(R.id.pilih1);
		RadioButton r2 = (RadioButton) formElementView
				.findViewById(R.id.pilih2);

		r1.setText("Publik");
		r2.setText("Teman satu area kerja");
		builder.setView(formElementView).setCustomTitle(view)
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						String toastString = "";
						int selectId = pilihan.getCheckedRadioButtonId();

						RadioButton selectedRadio = (RadioButton) formElementView
								.findViewById(selectId);

						toastString += "Selected radio button is: "
								+ selectedRadio.getText() + "!\n";
						showToast(toastString);
						dialog.dismiss();
						TextView t1 = (TextView) findViewById(R.id.textPrivacy);
						t1.setText(selectedRadio.getText().toString());
					}

				}).show();

	}

	public void showToast(String customText) {
		Toast.makeText(this, customText.trim(), Toast.LENGTH_LONG).show();
	}
}
